//------------------------------------------------------------------------------
//  <copyright file="Microsoft.Robotics.Manipulation.KukaPipelineAPI.h" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
//  </copyright>
//------------------------------------------------------------------------------
#pragma once
#include <KukaLBR4PlusController.h>
#include "FeedbackMessageManaged.h"
#include "WorkingRegisterMessageManaged.h"
#include "IManagedFRI.h"
#include "ManagedKukaFriThunk.h"

using namespace System;
using namespace System::Collections::Generic;
using namespace System::Runtime::InteropServices;

namespace Microsoft 
{
    namespace Robotics
    {
        namespace Hardware
        {
            namespace KukaLBR
            {
                /// Managed API to the controller pipeline. Exposes the unmanaged control messages
                // to managed clients as API calls
                /// Currently the only controller available has some Kuka arm dependencies 
                /// for example the registers are kuka specific
                public ref class KukaPipelineAPI : IDisposable
                {
                public:
                    /// <summary> Creates a PipeLineApi class with callbacks to a managed FRI. </summary>
                    /// <param name="managedFRIConnection"> Managed FRI handler </param>
                    /// Object Creation
                    static KukaPipelineAPI^ Create(IManagedFRI^ managedApi);

                    /// <summary> Destructor </summary>
                    ~KukaPipelineAPI();

					/// <summary> Finalizer </summary>
					!KukaPipelineAPI();

					/// <summary> Used by dtor and finalizer to clean up unamanged resources </summary>
					void CleanUpUnmanagedResources();

                    /// <summary> Set the controller state machine</summary>
                    /// <param name="pStateMachine">Pointer to state machine </param>
                    /// <returns> Return code  true for no error </returns>
                    bool SetStateMachineMessage(void *pStateMachine);

                    /// <summary> Set the controller state machine</summary>
                    /// <param name="pStateMachine">Pointer to state machine </param>
                    /// <returns> Return code  true for no error </returns>
                    bool SetStateMachineMessage(ControllerStateMachineDescription *pStateMachine);

                    /// <summary>Query for controller feedback registers</summary>
					/// <returns>Managed version of feedback registers or null if cannot get feedback registers <returns>
                    FeedbackMessageManaged^ GetFeedbackMessage();

                    /// <summary> Set the controller working registers</summary>
                    /// <param name="pStateMachine">Register values </param>
                    /// <returns> Return code  true for no error </returns>
                    bool SetWorkingRegisters(WorkingRegisterMessageManaged ^workingRegisters);

                    /// <summary>Start the controller state machine</summary>
                    /// <returns> Return code  true for no error </returns>
                    bool StartStateMachine();

                    /// <summary>Stop the controller state machine</summary>
                    /// <returns> Return code  true for no error </returns>
                    bool StopStateMachine();

                    /// <summary>Close the underlying FRI connection</summary>
                    /// <returns> Return code  true for no error </returns>
                    bool CloseConnection();

                private:
                    static volatile long creationCount;
                    static KukaPipelineAPI ^ singleton = nullptr;

                    /// <summary> Initializes a new instance of the <c>KukaPipelineAPI</c> class with callbacks to a managed FRI. </summary>
                    /// <param name="managedFRIConnection"> Managed FRI handler </param>
                    KukaPipelineAPI(IManagedFRI^ managedApi);


                    /// <summary> Initialize the API </summary>
                    /// <param name="managedApi">Optional managed FRI server. If null uses the default unmanaged FRIserver </param>
                    void Initialize(IManagedFRI^ managedApi);

                    // Thunk layer to a managed FRI - only used when managed FRI is called for
                    IManagedFRI^ kukaThunkFri;

                    // Wrapped controller
                    KukaLBR4PlusController *pController;

                    // Message handler for the controller
                    ControllerClientMessageHandler *pClientMessageHandler;
                };
            }
        }
    }
}
